package com.dcy.znoa.controller.attendance;

import com.dcy.znoa.common.annotation.AuthenticationAPI;
import com.dcy.znoa.common.controller.BaseController;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.attendance.model.attUserRule.AURBindModel;
import com.dcy.znoa.controller.attendance.model.attUserRule.SelectAURBindModel;
import com.dcy.znoa.domain.Oper;
import com.dcy.znoa.domain.attendance.AttRule;
import com.dcy.znoa.service.attendance.AttUserRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

import static com.dcy.znoa.common.util.pojo.JsonCommon.SUCCESS_CODE;

/**
 * 考勤规则与员工关联表
 * @author pengyu
 */
@Controller
@RequestMapping("attUserRule")
public class AttUserRuleController extends BaseController {

    @Autowired
    AttUserRuleService attUserRuleService;

    //WEB 查询已绑定用户
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectBind")
    public JsonForList selectBind(@Valid SelectAURBindModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonForList("404",bindingResult.getFieldError().getDefaultMessage());
        }
        AttRule cp = new AttRule();
        cp.setUpdateManId(getCurrentUser().getOperId());
        cp.setUpdateTime(new Date());
//        cp.setValidFlag(0);
        JsonForList jsonForList = attUserRuleService.selectBind(model);
        return jsonForList;
    }

    //WEB 查询未绑定用户，我这里默认把默认规则的排前面算了
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectUnBind")
    public JsonForList selectUnBind(@Valid SelectAURBindModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonForList("404",bindingResult.getFieldError().getDefaultMessage());
        }
        AttRule cp = new AttRule();
        cp.setUpdateManId(getCurrentUser().getOperId());
        cp.setUpdateTime(new Date());
//        cp.setValidFlag(0);
        JsonForList jsonForList = attUserRuleService.selectUnBind(model);
        return jsonForList;
    }

    //WEB 规则与用户绑定 ,sql 即 insert
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/bind")
    public JsonCommon bind(@Valid AURBindModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        AttRule cp = new AttRule();
        cp.setUpdateManId(getCurrentUser().getOperId());
        cp.setUpdateTime(new Date());
//        cp.setValidFlag(0);
        attUserRuleService.addBatch(model);
        return new JsonCommon(SUCCESS_CODE);
    }

    //WEB 规则与用户解绑 SQL 即 delete
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/unBind")
    public JsonCommon unBind(@Valid AURBindModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        AttRule cp = new AttRule();
        cp.setUpdateManId(getCurrentUser().getOperId());
        cp.setUpdateTime(new Date());
//        cp.setValidFlag(0);
        attUserRuleService.delBatch(model);
        return new JsonCommon(SUCCESS_CODE);
    }

}
